package com.vip.fdfs.util;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.Result;
import com.vip.common.response.Results;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件工具
 *
 * @author wgb
 * @date 2019/12/22 17:31
 */
@Slf4j
@Component
public class FileDFSUtils {
    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    /**
     * 上传图片
     */
    public Result<Map<String, String>> upload(MultipartFile multipartFile) {
        try {
            String originalFilename = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
            StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), originalFilename, null);
            //缩略图的路径
            String thumbImagePath = storePath.getGroup() + "/" + thumbImageConfig.getThumbImagePath(storePath.getPath());
            //全路径(带组)
            String fullImagePath = storePath.getFullPath();
            Map<String, String> map = new HashMap<>();
            map.put("thumb_image_path", thumbImagePath);
            map.put("full_image_path", fullImagePath);
            return Results.newSuccessResult(map, "文件上传成功");
        } catch (Exception e) {
            log.error("文件上传出错", e);
        }
        return Results.newFailResult(ErrorCode.FILE_UPLOAD, "文件上传失败");
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     */
    public Result<String> uploadFile(MultipartFile multipartFile) {
        try {
            String originalFilename = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
            StorePath storePath = this.storageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), originalFilename, null);
            //全路径(带组)
            String fullImagePath = storePath.getFullPath();
            return Results.newSuccessResult(fullImagePath, "文件上传成功");
        } catch (Exception e) {
            log.error("文件上传出错", e);
        }
        return Results.newFailResult(ErrorCode.FILE_UPLOAD, "文件上传失败");
    }

    /**
     * 下载文件
     *
     * @param fileUrl 文件URL
     * @return 文件字节
     */
    public InputStream downloadFile(String fileUrl) {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        return storageClient.downloadFile(group, path, ins -> ins);
    }

    /**
     * 查看文件
     *
     * @param fileUrl
     * @return
     */
    public byte[] viewFile(String fileUrl) {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        return storageClient.downloadFile(group, path, downloadByteArray);
    }

    /**
     * 删除文件
     *
     * @param fileUrl
     */
    public Result<String> deleteFile(String fileUrl) {
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
            return Results.newSuccessResult("", "文件删除成功");
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return Results.newFailResult(ErrorCode.FILE_UPLOAD, "文件删除失败");
    }

}
